package pinboard.dataLayer.potocol;

public class Guid {

    String value = null;

    public static Guid newBlank() {
        return new Guid(new byte[16]);
    }

    public Guid(byte[] input) {
        value = getString(input);
    }

    public Guid(String input) {
        this.value = input;
    }

    String getString(byte[] input) {
        String[] rawArr = new String[16];
        for (int i = 0; i < input.length; i++) {
            int hex = input[i];
            if (hex < 0) {
                hex = hex + 256;
            }
            rawArr[i] = Integer.toHexString(hex);
            if (rawArr[i].length() < 2) {
                rawArr[i] = "0" + rawArr[i];
            }
        }
        String[] adjustArr = new String[rawArr.length];
        adjustArr[0] = rawArr[3];
        adjustArr[1] = rawArr[2];
        adjustArr[2] = rawArr[1];
        adjustArr[3] = rawArr[0];
        adjustArr[4] = rawArr[5];
        adjustArr[5] = rawArr[4];
        adjustArr[6] = rawArr[7];
        adjustArr[7] = rawArr[6];
        adjustArr[8] = rawArr[8];
        adjustArr[9] = rawArr[9];
        adjustArr[10] = rawArr[10];
        adjustArr[11] = rawArr[11];
        adjustArr[12] = rawArr[12];
        adjustArr[13] = rawArr[13];
        adjustArr[14] = rawArr[14];
        adjustArr[15] = rawArr[15];
        String result = link(adjustArr, 0, 4) + "-" + link(adjustArr, 4, 2) + "-" +
                link(adjustArr, 6, 2) + "-" + link(adjustArr, 8, 2) + "-" + link(adjustArr, 10, 6);
        return result;

    }

    String link(String[] arr, int start, int len) {
        String result = "";
        for (int i = start; i < start + len; i++) {
            result = result + arr[i];
        }
        return result;
    }

    @Override
    public String toString() {
        return value;
    }

    public byte[] toByteArray() {
        return toByteArray(value);
    }

    byte[] toByteArray(String guidStr) {
        guidStr = guidStr.replaceAll("-", "");
        byte[] rawArr = new byte[16];
        for (int i = 0; i < guidStr.length(); i = i + 2) {
            String numStr = String.valueOf(guidStr.charAt(i)) + String.valueOf(guidStr.charAt(i + 1));
            int tmp = Integer.parseInt(numStr, 16);
            if (tmp > 128) {
                tmp = tmp - 256;
            }
            rawArr[i / 2] = (byte) tmp;
        }
        byte[] adjustArr = new byte[16];
        adjustArr[0] = rawArr[3];
        adjustArr[1] = rawArr[2];
        adjustArr[2] = rawArr[1];
        adjustArr[3] = rawArr[0];
        adjustArr[4] = rawArr[5];
        adjustArr[5] = rawArr[4];
        adjustArr[6] = rawArr[7];
        adjustArr[7] = rawArr[6];
        adjustArr[8] = rawArr[8];
        adjustArr[9] = rawArr[9];
        adjustArr[10] = rawArr[10];
        adjustArr[11] = rawArr[11];
        adjustArr[12] = rawArr[12];
        adjustArr[13] = rawArr[13];
        adjustArr[14] = rawArr[14];
        adjustArr[15] = rawArr[15];
        return adjustArr;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        return this.toString().equals(o.toString());
    }
}
